package com.lianbang.pharmacy.module.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.SpanUtils
import com.google.android.flexbox.FlexboxLayoutManager
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.adapter.SignListAdapter
import com.lianbang.pharmacy.bean.SignBean
import com.lianbang.pharmacy.bean.UserSignBean
import com.lianbang.pharmacy.databinding.ActivitySignBinding
import com.lianbang.pharmacy.dialog.SignDialog
import com.lianbang.pharmacy.vm.MainVM
import com.lianbang.pharmacy.vm.SignVM
import com.lianbang.pharmacy.vm.UserVM
import kotlinx.android.synthetic.main.activity_sign.*

/**
 * Created by maoqi on 2020/12/16.
 */
class SignActivity : BaseActivity<SignVM, ActivitySignBinding>() {

    companion object {

        const val REQ_CODE = 1001

        fun start(fragment: Fragment) {
            val starter = Intent(fragment.context, SignActivity::class.java)
            fragment.startActivityForResult(starter, REQ_CODE)
        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_sign
    }

    override fun initData(intent: Intent) {
        mViewModel.mSignLiveData.observe(this, Observer {
            when (it) {
                is UserSignBean -> {
                    tv_score.text = SpanUtils().append("${it.sum_integral}")
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.dp_46))
                        .setBold().append("积分").setFontSize(ResourcesUtils.getDimens(R.dimen.dp_12))
                        .create()
                    tv_sign.text = SpanUtils().append("本周签到")
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.dp_14))
                        .append("${it.sign_num}")
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.dp_22))
                        .setBold()
                        .append("天（缺勤${7 - it.sign_num}天）")
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.dp_14))
                        .create()
                    rv_list.layoutManager = GridLayoutManager(this, 7)
                    rv_list.adapter = SignListAdapter(it.sign_num).run {
                        replaceData(it.sign_list)
                        this
                    }
                }
                is SignBean -> {
                    addFragment(SignDialog.newInstance(it.integral))
                    mViewModel.userSignInfo()
                    setResult(Activity.RESULT_OK)
                }
            }
        })
    }

    override fun initView() {
        btn_sign.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                mViewModel.sign()
            }
        })

    }

    override fun loadData() {
        mViewModel.userSignInfo()
    }

    override fun setToolbarTitle(): String? {
        return "签到积分"
    }

}